package com.lej.core.widget.customview

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs


class HorizontalRecyclerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {
    private var enableLog = false

    ///1按下，2,3,4 比对过滑动方向，5往下滑动
    var status: Int = 0

    ///重复 count-2 次
    val STATUS_COUNT_NUM = 3
    private var startX: Int = 0
    private var startY: Int = 0
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                startX = ev.x.toInt()
                startY = ev.y.toInt()
                status = 1
            }
            MotionEvent.ACTION_MOVE -> {
                val endX = ev.x.toInt()
                val endY = ev.y.toInt()
                val disX: Int = abs(endX - startX)
                val disY: Int = abs(endY - startY)
                if (status < STATUS_COUNT_NUM - 1) {
                    if (disY > disX) {
                        "disY:$disY > disX:$disX".logI("横向recyclerView")
                        status = STATUS_COUNT_NUM
                    } else {
                        status++
                    }
                } else if (status == STATUS_COUNT_NUM) {
                    val dy: Int = startY - ev.y.toInt()
                    val scrollOffset = intArrayOf(0, 0)

                    "dispatchNestedPreScroll:$dy".logI("横向recyclerView")
                    dispatchNestedScroll(0, 0, 0, dy, scrollOffset)
                }
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                "startNestedScroll:SCROLL_AXIS_VERTICAL".logI("横向recyclerView")
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
            }
            else -> {
                if (status == STATUS_COUNT_NUM) {
                    super.onInterceptTouchEvent(ev)
                    return true
                }
            }
        }

        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(e: MotionEvent): Boolean {
        when (e.action) {
            MotionEvent.ACTION_DOWN -> {
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
            }
            MotionEvent.ACTION_MOVE -> {
                if (status == STATUS_COUNT_NUM) {
                    val dy: Int = startY - e.y.toInt()
                    "消费了事件:$dy".logI("横向recyclerView")
                    return true
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                "stopNestedScroll${e.action}".logI("横向recyclerView")
                stopNestedScroll()
            }
        }
        return super.onTouchEvent(e)

    }

    private infix fun String.logI(tag: String) = apply { if (enableLog) Log.i("sss_$tag", this) }
}
